# Utility function for processing various files

# Reads file with ratings and produces
# hash with the structure {playerNickName => rating}
def getRatingsFromFile(fileName)
  ratings = {}
  fin = File.new(fileName, "r")
  fin.readlines.each {|line| ratings[$1] = $2.to_f if(/^(.+?)\s+([\d.]+)/.match(line))}	
  fin.close
  return ratings
end

# Reads file with ratings and produces
# hash with the structure {playerNickName => rating} where rating is integer value
def getIntegerRatingsFromFile(fileName)
  # Not the "ruby way"? give me an idea how to change it!
  resMap = {}
  getRatingsFromFile(fileName).each_pair {|key, value| resMap[key] = value.round }
  resMap
end

def getSortedIntegerRatingsFromFile(fileName)  
  resMap = {}
  getRatingsFromFile(fileName).each_pair {|key, value| resMap[key] = value.round }
  resMap
end

# Creates the list of players mentioned in the list of played games
def getPlayersFromGames(fileName)
  players = []
  gamesFile = File.new(fileName, "r")
  gamesFile.readlines.each {|line|
    if(/^\s*([^<>]+\w)\s*([<>])\s*([^<>]+\w)\s*$/.match(line))	  
      players << $1
      players << $3
    end
  }
  return players.uniq!
end

# just reads players from the plain text file
def getPlayersFromList(fileName)
  playersFile = File.new(fileName, "r")
  players = playersFile.readlines;
  playersFile.close;
  players.each {|player| player.rstrip!.lstrip! }
  players;
end

# finds date in the name of the file with ratings and returns 
# it in the form dd/mm
def parseTourneyDate(fileName)	
	str = ""
	str = "#{$2}/#{$1}" if(/^\w*-\d+-(\d+)-(\d+).*$/.match(fileName))
	str
end